<?php

// +----------------------------------------------------------------------
// | ShintianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2011 Shintian All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: ljt <ljt1002@gmail.com>
// +----------------------------------------------------------------------
// 后台用户模块
class UserAction extends BaseAction {

    function _filter(&$map) {
        $map['id'] = array('egt', 2);
        $map['account'] = array('like', "%" . $_POST['account'] . "%");
    }

    // 检查帐号
    public function checkAccount() {
        if (!preg_match('/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/', $_POST['account'])) {
            $this->error(L('_USERNAME_FORBID_'));
        }
        $User = M("User");
        // 检测用户名是否冲突
        $name = $_REQUEST['account'];
        $result = $User->getByAccount($name);
        if ($result) {
            $this->error(L('_USER_UNIQUE_'));
        } else {
            $this->success(L('_USER_CANUSE_'));
        }
    }

    // 插入数据
    public function insert() {
        // 创建数据对象
        $User = D("User");
        if (!$User->create()) {
            $this->error($User->getError());
        } else {
            // 写入帐号数据
            if ($result = $User->add()) {
                $this->addRole($result, $_POST['role_id']);
                $this->success(L('_INSERT_SUCCESS_'));
            } else {
                $this->error(L('_INSERT_ERROR_'));
            }
        }
    }

    function edit() {
        $model = M('User');
        $id = $_REQUEST ['id'];
        $vo = $model->getById($id);
        $roleDao = D('Role');
        $role = $roleDao->where('status=1')->findall();
        $ruDao = D('RoleUser');
        $roleuser = $ruDao->where('user_id=' . $id)->find();
        $this->assign('roleuser', $roleuser);
        $this->assign('role', $role);
        $this->assign('vo', $vo);
        $this->display();
    }

    function add() {
        $rmodel = M('Role');
        $allrole = $rmodel->select();
        $this->assign('allrole', $allrole);
        $this->display();
    }

    protected function addRole($userId, $roleId) {
        //新增用户自动加入相应权限组
        $RoleUser = M("RoleUser");
        $RoleUser->user_id = $userId;
        // 默认加入网站编辑组
        $RoleUser->role_id = $roleId;
        $RoleUser->add();
    }

    //重置密码
    public function resetPwd() {
        $id = $_POST['id'];
        $password = $_POST['password'];
        if ('' == trim($password)) {
            $this->error(L('_PASSWORD_REQUIRE_'));
        }
        $User = M('User');
        $User->password = md5($password);
        $User->id = $id;
        $result = $User->save();
        if (false !== $result) {
            $this->success(L('_RESETPASS_SUCCESS_') . $password);
        } else {
            $this->error(L('_RESETPASS_ERROR_'));
        }
    }

}

?>